package cn.oms.manage.dao.persist.repository.impl;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.util.PageInfoToPageDataConverter;
import cn.oms.manage.dao.persist.mapper.DepartmentMapper;
import cn.oms.manage.dao.persist.repository.IDepartmentRepository;
import cn.oms.manage.pojo.entity.Department;
import cn.oms.manage.pojo.vo.DepartmentListVO;
import cn.oms.manage.pojo.vo.DepartmentStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 说明：
 *
 * @author: Fcx
 * @date: 2023/6/19
 **/

@Slf4j
@Repository
public class DepartmentRepositoryImpl implements IDepartmentRepository {

    @Autowired
    DepartmentMapper departmentMapper;
    
    public DepartmentRepositoryImpl() {
        log.debug("创建数据访问实现类对象：DepartmentRepositoryImpl");

    }

    @Override
    public int insert(Department department) {
        log.debug("开始执行【插入订单】的数据访问，参数：{}", department);
        return departmentMapper.insert(department);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除用户】的数据访问，参数：{}", id);
        return departmentMapper.deleteById(id);
    }

    @Override
    public int countByName(String name) {
        log.debug("开始执行【统计匹配名称的用户的数量】的数据访问，参数：{}", name);
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return departmentMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByNameAndNotId(Long id, String name) {
        log.debug("开始执行【统计匹配名称的用户的数量】的数据访问，参数：id:{},名称:{}",id, name);
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).ne("id",id);
        return departmentMapper.selectCount(queryWrapper);
    }

    @Override
    public int update(Department department) {
        log.debug("开始执行【更新用户】的数据访问，参数：{}", department);
        return departmentMapper.updateById(department);
    }

    @Override
    public DepartmentStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询用户信息】的数据访问，参数：{}", id);
        return departmentMapper.getStandardById(id);
    }

    @Override
    public PageData<DepartmentListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询用户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DepartmentListVO> list = departmentMapper.list();
        PageInfo<DepartmentListVO> pageInfo = new PageInfo<>(list);
        PageData<DepartmentListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
